var config = require("config");
cc.Class({
    extends: cc.Component,

    properties: {
        xMinSpeed: {
            default:0,
            type:cc.Integer,
            tooltip: 'x轴最小速度',
        },
        xMaxSpeed: {
            default:0,
            type:cc.Integer,
            tooltip: 'x轴最大速度',
        },
        yMinSpeed: {
            default:0,
            type:cc.Integer,
            tooltip: 'y轴最小速度',
        },
        yMaxSpeed: { 
            default:0,
            type:cc.Integer,
            tooltip: 'y轴最大速度',
        },
        initHP: {
            default:0,
            type:cc.Integer,
            tooltip: '初始生命值',
        }, 
        initSpriteFrame:{
            default: null,
            type: cc.SpriteFrame,
            tooltip: '初始化的图像'
        }, 
        initScore: {
            default:0,
            type:cc.Integer,
            tooltip: '死后获得的分数'
        },
        sprArray :{
            default:[],
            type:[cc.SpriteFrame],
        },
        propType:{
            default:0,
            type:cc.Integer,
            tooltip: '怪物类型'
        },
    },

    // use this for initialization
    onLoad: function () {
        cc.director.getCollisionManager().enabled=true;
        // this.xSpeed = Math.random()*(this.xMaxSpeed-this.xMinSpeed) + this.xMinSpeed;
        // this.ySpeed = this.yMinSpeed-500;
        this.propGroup = this.node.parent.getComponent('propGroup');

        // this.collider = this.getComponent(cc.PolygonCollider);
        // this.collider.points = [cc.v2(-50,-50),cc.v2(50,-50),cc.v2(50,50),cc.v2(-50,50)];
        
        //碰撞框
        this.speedY = 1200;
        this.speedX = 0; 
        this.ay = -3000;
        this.ax = 0;

        this.isToHero = config.isToHero;//飞向英雄
        if(this.isToHero){
            this.speedY = 200;//200
            this.aTohero = 2000; //4000
            this.ay = 0;
            this.ax = 0;
            this.speedX = 0; 
        }
    },
    init: function(){
        if (this.node.group != 'prop'){
            this.node.group = 'prop';
        }
        if (this.hP != this.initHP){
            this.hP = this.initHP;
        }
        
        if (this.score != this.initScore){
            this.score = this.initScore;
        }

        if (this.propIndex != this.propType){
            this.propIndex = this.propType;
        }

        var nSprite = this.node.getComponent(cc.Sprite);
        // var ranNum = Math.floor(Math.random()*100);
        // if(ranNum < 70){
        //     this.propType = 1;
        // }else{
        //     this.propType = 2;
        // }

        if (this.propType == 1){//金币道具 
            this.sprIndex = Math.floor(Math.random()*7);
            nSprite.spriteFrame = this.sprArray[this.sprIndex];
            this.propIndex = 10 + this.sprIndex;
        }else if(this.propType == 2){//特殊道具
            this.sprIndex = Math.floor(Math.random()*4);
            if(this.sprIndex == 3){
                if(config.LaserNum < 3 ){
                }else{
                    this.sprIndex = 1;
                }
            }
            nSprite.spriteFrame = this.sprArray[this.sprIndex];
            this.propIndex = 20 + this.sprIndex;
        }

        //碰撞框
        this.speedY = 1200;
        this.speedX = 0; 
        this.ay = -3000;
        this.ax = 0;

        this.isToHero = config.isToHero;//飞向英雄
        if(this.isToHero){
            this.speedY = 200;
            this.aTohero = 3000;
            this.ay = 0;
            this.ax = 0;
            this.speedX = 0; 
        }

        // console.log("this.propGroup.getHeroPos():",this.propGroup.getHeroPos().x);
    },
    update: function (_dt) {
        if (this.propGroup.eState != D.commonInfo.gameState.start){
            return ;
        }
        
        // if(config.HeroState == 1){ 
        //     this.node.y += dt*this.ySpeed-5;
        // }else{
        //     this.node.y += dt*this.ySpeed;
        // }
        // this.node.x += dt*this.xSpeed;

        var disX = 0;
        var disY = 0;
        this.isToHero = config.isToHero;
        if(this.isToHero){
            var herox = this.propGroup.getHeroPos().x;
            var heroy = this.propGroup.getHeroPos().y;

            
            var distoherox = this.node.x - herox;
            var distoheroy = heroy - this.node.y;

            if(Math.abs(distoheroy) != 0){ //< 700
                var atoherox = -(this.aTohero*(distoherox/Math.sqrt(Math.pow(distoherox,2)+Math.sqrt(Math.pow(distoheroy,2)))) + this.ax);
                var atoheroy = this.aTohero*(distoheroy/Math.sqrt(Math.pow(distoherox,2)+Math.sqrt(Math.pow(distoheroy,2)))) + this.ay;
                var spY = this.speedY + atoheroy * _dt;
                disY = (spY*spY - this.speedY*this.speedY)/(2*atoheroy) ;
                this.speedY = spY
                var spX = this.speedX + atoherox * _dt;
                //console.log("像英雄的横向速度>>>>"+spX );
                disX = (spX*spX - this.speedX*this.speedX)/(2*atoherox) ;
                this.speedX = spX
            }else{
                disY = -100;
            }
        }else{
            disX =  this.speedX * _dt;
            if(this.ay != 0){
                var spY = this.speedY + this.ay * _dt;
                disY = (spY*spY - this.speedY*this.speedY)/(2*this.ay);
                this.speedY = spY
                
            }else{
                disY = this.speedY * _dt ;

                if(disY>0)//test
                    disY = -100;
            }
        }
        this.node.x += disX;
        // if(this.isToHero && this.node.y < heroy){
        //     if(disY>0){
        //         this.node.y += disY;
        //     }    
        // }else{
            this.node.y += disY;
        // }
        
        //出屏幕后 回收节点
        if (this.node.y < -this.node.parent.height/2){
            this.propGroup.enemyDied(this.node,0);
        }
    },
    //碰撞检测
    onCollisionEnter: function(other,self){
        if (other.node.group != 'hero'){
            return ;
        }
        
        if (this.hP>0){//防止再次碰撞
            this.hP -= 1;
        } else {
            return ;
        }
        if (this.hP <= 0 && config.HeroState != 2){
            this.node.group = 'default'; //不让动画在执行碰撞
            for(var i=0;i<config.goldValue.length;i++){
                var index = 10 + i;
                if(index == this.propIndex){
                    this.score = config.goldValue[i];
                }
            }

            if(this.propIndex == config.propIndexs.onrush){
                if(config.HeroState == 0){
                    this.propGroup.getOnrush();
                    cc.vv.audioMgr.playSFX("get_invincible");
                } 
            }else if(this.propIndex == config.propIndexs.doubleBullet){
                this.propGroup.getDoubleBullet();
                cc.vv.audioMgr.playSFX("get_item");
            }else if(this.propIndex == config.propIndexs.magnet){
                this.propGroup.getMagnet();
                cc.vv.audioMgr.playSFX("get_item");
            }else if(this.propIndex == config.propIndexs.laser){
                this.propGroup.getLaser();
                cc.vv.audioMgr.playSFX("get_item");
            }else if(this.propIndex == config.propIndexs.seawave){ 
            }else{
                cc.vv.audioMgr.playSFX("get_coin");
            }
            this.propGroup.enemyDied(this.node, this.score);
        }
    },
    //动画结束后 动画节点回收
    onFinished: function(event) { 
        this.propGroup.enemyDied(this.node, this.score);
    },
    //播放死亡动画
    playDieAni(){
        this.hP = 0;
        this.node.group = 'default'; //不让动画在执行碰撞
        var anim = this.getComponent(cc.Animation);
        var animName = 'enemy_ani';
        anim.play(animName);
        anim.on('finished',  this.onFinished, this);
    },
    
});
